package com.eascs.dubbo.api.loanAccount;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.account.api.ICapitalFlowService;
import com.eascs.account.api.constants.enums.LoanAccountType;
import com.eascs.admin.api.loanAccount.TransferService;
import com.eascs.admin.common.vo.ModelResult;
import com.eascs.admin.o2o.backfee.service.O2OComBankFeeService;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.loanAccount.entity.O2oCollectFee;
import com.eascs.admin.o2o.loanAccount.entity.O2oLoanapplyAccountEntity;
import com.eascs.admin.o2o.loanAccount.service.Io2oLoanapplyAccountService;
import com.eascs.app.dao.PageResult;

@Service
public class TransferServiceImpl implements TransferService{
	
	@Autowired
	com.eascs.admin.o2o.loanAccount.service.IO2oCollectFeesService iO2oCollectFeesService;
	
	@Autowired
	Io2oLoanapplyAccountService o2oLoanapplyAccountService;
	
	@Reference
	ICapitalFlowService capitalFlowService;
	
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;
	
	@Autowired
	O2OComBankFeeService o2oComBankFeeService;

	@Override
	public O2oCollectFee getById(String id) {
		return iO2oCollectFeesService.queryById(id);
	}

	@Override
	public String delO2oCollectFee(String id) {
		O2oCollectFee  entity = iO2oCollectFeesService.queryById(id);
		entity.setIsDel(StatusContexts.O2O_YES);
		int count = iO2oCollectFeesService.update(entity);
		if(count<=0){
			return "删除失败";
		}
		return "删除成功";
	}

	@Override
	public String startUp(String id) {
		O2oCollectFee entity = iO2oCollectFeesService.queryById(id);
		if(entity.getCollectMoney()==null){
			return "打款金额不能为空";
		}
		if(entity.getBreakDate()==null){
			return "打款时间不能为空";
		}
		ModelResult<Boolean> modelResult = iO2oCollectFeesService.startup(entity, "", "");
		if (modelResult.isSuccess() && modelResult.getModel()) {
			return null;
		} else {
			return modelResult.getErrMsg();
		}
	}

	@Override
	public PageResult<Object[]> queryManageAccount(String loanapplyNo) {
		PageResult<Object[]> pageResult = new PageResult<Object[]>();
		O2oLoanapplyAccountEntity loanapplyAccountEntity = o2oLoanapplyAccountService
				.getEntityByLoanapplyNo(loanapplyNo);
		if(loanapplyAccountEntity!=null){
			com.eascs.workflow.common.vo.ModelResult<PageResult<Object[]>> glfResult = capitalFlowService.loanAccountPage(loanapplyAccountEntity.getAccountNo(),
					LoanAccountType.ISTRATION, 1);
			if (glfResult.isSuccess() && glfResult.getModel() != null) {
				pageResult = glfResult.getModel();
			}
		}
		return pageResult;
	}

	@Override
	public PageResult<Object[]> queryRiskAccount(String loanapplyNo) {
		PageResult<Object[]> pageResult = new PageResult<Object[]>();
		O2oLoanapplyAccountEntity loanapplyAccountEntity = o2oLoanapplyAccountService
				.getEntityByLoanapplyNo(loanapplyNo);
		if(loanapplyAccountEntity!=null){
			com.eascs.workflow.common.vo.ModelResult<PageResult<Object[]>> fxbcjResult = capitalFlowService.loanAccountPage(loanapplyAccountEntity.getAccountNo(),
					LoanAccountType.COMPENSATION, 1);
			if (fxbcjResult.isSuccess() && fxbcjResult.getModel() != null) {
				pageResult = fxbcjResult.getModel();
			}
		}
		return pageResult;
	}

	@Override
	public PageResult<Object[]> queryAssureAccount(String loanapplyNo) {
		PageResult<Object[]> pageResult = new PageResult<Object[]>();
		O2oLoanapplyAccountEntity loanapplyAccountEntity = o2oLoanapplyAccountService
				.getEntityByLoanapplyNo(loanapplyNo);
		if(loanapplyAccountEntity!=null){
			com.eascs.workflow.common.vo.ModelResult<PageResult<Object[]>> fxbcjResult =capitalFlowService.loanAccountPage(loanapplyAccountEntity.getAccountNo(),
					LoanAccountType.DEPOSIT, 1);
			if (fxbcjResult.isSuccess() && fxbcjResult.getModel() != null) {
				pageResult = fxbcjResult.getModel();
			}
		}
		return pageResult;
	}

	@Override
	public LoanapplyExpand queryDetail(String billCode) {
		O2oLoanapplyEntity loan = o2oLoanapplyService.fingByBillCode(billCode);
		LoanapplyExpand loanapplyExpand = loanapplyExpandService.queryLoanapplyNo(loan.getLoanapplyNo());
		if(loanapplyExpand!=null){
			loanapplyExpand =o2oComBankFeeService.sumn(loanapplyExpand);
			return loanapplyExpand;
		}else{
			return null;
		}
	}

}
